<template>
  <div style="margin: 10px">
    <Screen :category="category" @query="query" />
    <div style="margin-bottom: 10px">
      <el-button
        type="primary"
        @click="$router.push('/diaryAddEdit')"
      >添加文章</el-button>
    </div>
    <Tabel
      :columns="columns"
      :table-data="aricleList"
      :loading="loading"
      :page-index="queryForm"
      @handleCurrent="handleCurrent"
    >
      <el-table-column label="图片" width="90">
        <template slot-scope="scope">
          <img :src="scope.row.description_image_path" class="articleImag">
        </template>
      </el-table-column>
      <el-table-column slot="handleState" label="设置热门">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.hot"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
            @change="changeSwitch(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        slot="handleColumn"
        label="操作"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="articleEdit(scope.row)"
          >文章编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelArticle(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </Tabel>
    <!-- 分页器 -->
    <Pagination
      :total="total"
      @pagination="pagination"
      @handleSizeChange="handleSizeChange"
    />
  </div>
</template>
<script>
const Screen = () => import('./components/screen')
const Tabel = () => import('@/components/Table')
const Pagination = () => import('@/components/Pagination')
import { article, changeSwitch, delArticle } from '@/api/article'
// import { getNowFormatTime } from '@/util.js'

// import Tabel from '@/components/Table';
export default {
  name: 'Diary',
  components: {
    Screen,
    Tabel,
    Pagination
  },
  data() {
    return {
      loading: false, // 加载
      columns: [
        {
          prop: 'title',
          label: '文章标题'
        },
        {
          prop: 'category_name',
          label: '标签'
        },
        {
          prop: 'webPageName',
          label: '页面名称'
        },
        {
          prop: '点赞数',
          label: '点赞数'
        },
        {
          prop: 'webPageName',
          label: '浏览数'
        }
      ],
      aricleList: [], // 文章列表
      total: 0,
      category: [], // 标签分类
      queryForm: {
        size: 10,
        current: 1,
        webPageId: 1
      }
    }
  },
  created() {
    this.query()
  },
  methods: {
    query(queryForm) {
      if (queryForm) {
        this.queryForm = queryForm
      }
      this.loading = true
      article(this.queryForm)
        .then((res) => {
          this.total = res.page.total
          this.aricleList = res.data
          this.category = res.category
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 删除文章
    handleDelArticle({ id }) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delArticle(id).then((res) => {
            this.$message.success('删除成功')
            this.query()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 文章编辑
    articleEdit(row) {
      this.$router.push('/diaryAddEdit?id=' + row.id)
    },
    // 分页事件
    pagination({ page: current, limit: size }) {
      this.queryForm.current = current
      this.query()
    },
    handleSizeChange({ page: current, limit: size }) {
      this.queryForm.size = size
      this.query()
    },
    // 热门修改
    changeSwitch(row) {
      changeSwitch({
        id: row.id,
        hot: row.hot
      }).then((res) => {
        // 判断是否设置成功
        if (res.code === 200) {
          this.$message.success(res.msg)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 选中行
    handleCurrent() {}
  }
}
</script>
<style scoped>
.articleImag {
  width: 100%;
}
</style>
